package com.healthbox.appointmentservice.service.impl;

import com.healthbox.appointmentservice.DTO.LimitQueryDTO;
import com.healthbox.appointmentservice.DVO.AppointmentInfoDVO;
import com.healthbox.appointmentservice.DVO.AppointmentLimitDVO;
import com.healthbox.appointmentservice.entity.AppointmentInfo;
import com.healthbox.appointmentservice.entity.AppointmentLimit;
import com.healthbox.appointmentservice.mapper.AppointmentInfoMapper;
import com.healthbox.appointmentservice.mapper.AppointmentLimitMapper;
import com.healthbox.appointmentservice.service.AppointmentInfoQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pengpeng
 * @date 2022/5/12
 */
@Service
public class AppointmentInfoServiceImpl implements AppointmentInfoQueryService {
    @Autowired
    @Qualifier("limitRepoProxy")
    private AppointmentLimitMapper limitRepo;
    @Autowired
    private AppointmentInfoMapper infoRepo;
    @Override
    public List<AppointmentLimitDVO> queryAppointmentLimit(LimitQueryDTO query) {
        Integer index = query.getPageSize()==null || query.getPageIndex()==null ? null: query.getPageSize() * query.getPageIndex();
        List<AppointmentLimit> limits = limitRepo.findByDateAndHospitalId(query.getDate(), query.getDate(), query.getHospitalId(), query.getPageSize(), index);
        if (limits==null) return null;
        List<AppointmentLimitDVO> limitDVOS = new ArrayList<>();
        for (AppointmentLimit limit : limits) {
            int currentNumber = limitRepo.numberOfAppointment(limit.getALId());
            limitDVOS.add(AppointmentLimitDVO.fromEntity(limit, currentNumber));
        }
        return limitDVOS;
    }

    @Override
    public List<AppointmentInfoDVO> queryHistory(String userPhone, Long hospitalId, Long appointmentLimitId) {
        List<AppointmentInfo> historyInfo = infoRepo.findByUserPhoneAndHospitalIdAndALId(userPhone, hospitalId, appointmentLimitId);
        if (historyInfo==null) return null;
        List<AppointmentInfoDVO> limitDVOS  = new ArrayList<>();
        for (AppointmentInfo info : historyInfo) {
            limitDVOS.add(AppointmentInfoDVO.fromEntity(info));
        }
        return limitDVOS;
    }
}
